import Vue from 'vue'
import { axios } from '@/utils/request'

const api = {
    user: '/mock/api/user',
    role: '/mock/api/role',
    service: '/mock/api/service',
    permission: '/mock/api/permission',
    permissionNoPager: '/mock/api/permission/no-pager',
    previewDownloadToken:'/common/getDownloadPreviewToken'
}

export default api

//post
export function postAction(url, parameter) {
    return axios({
        url: url,
        method: 'post',
        data: parameter
    })
}

export async function postActionAsync(url, parameter) {
    return await axios({
        url: url,
        method: 'post',
        data: parameter
    })
}

//post method= {post | put}
export function httpAction(url, parameter, method) {
  if(method == 'get'){
    return getAction(url,parameter)
  }
    return axios({
        url: url,
        method: method,
        data: parameter
    })
}

//put
export function putAction(url, parameter) {
    return axios({
        url: url,
        method: 'put',
        data: parameter
    })
}

//get
export function getAction(url, parameter) {
    return axios({
        url: url,
        method: 'get',
        params: parameter
    })
}

export async function getActionAsync(url, parameter) {
    return await axios({
        url: url,
        method: 'get',
        params: parameter
    })
}

//deleteAction
export function deleteAction(url, parameter) {
    return axios({
        url: url,
        method: 'delete',
        params: parameter
    })
}

export function getUserList(parameter) {
    return axios({
        url: api.user,
        method: 'get',
        params: parameter
    })
}

export function getRoleList(parameter) {
    return axios({
        url: api.role,
        method: 'get',
        params: parameter
    })
}

export function getServiceList(parameter) {
    return axios({
        url: api.service,
        method: 'get',
        params: parameter
    })
}

export function getPermissions(parameter) {
    return axios({
        url: api.permissionNoPager,
        method: 'get',
        params: parameter
    })
}

// id == 0 add     post
// id != 0 update  put
export function saveService(parameter) {
    return axios({
        url: api.service,
        method: parameter.id == 0 ? 'post' : 'put',
        data: parameter
    })
}

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url, parameter) {
    return axios({
        url: url,
        params: parameter,
        method: 'get',
        responseType: 'blob'
    })
}
/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
 export function downFilePost(url, parameter) {
    return axios({
        url: url,
        data: parameter,
        method: 'post',
        responseType: 'blob'
    })
}
/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function postDownFile(url, data) {
    return axios({
        url: url,
        data: data,
        method: 'post',
        responseType: 'blob'
    })
}
/**
 * 下载文件
 * @param url 文件路径
 * @param fileName 文件名
 * @param parameter
 * @returns {*}
 */
export function downloadFile(url, fileName, parameter) {
    return downFile(url, parameter).then((data) => {
        if (!data || data.size === 0) {
            Vue.prototype['$message'].warning('文件下载失败')
            return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data]), fileName)
        } else {
            let url = window.URL.createObjectURL(new Blob([data]))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName)
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
        }
    })
}

/**
 * 下载文件 模板导出
 * @param url 文件路径
 * @param fileName 文件名
 * @param parameter
 * @param options Blob 可选项
 * @returns {*}
 */
export function downloadFileLink(url, fileName, parameter, options) {

    return downFile(url, parameter).then((data) => {
        if (!data || data.size === 0) {
            Vue.prototype['$message'].warning('文件下载失败')
            return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data], options), fileName)
        } else {
            let url = window.URL.createObjectURL(new Blob([data], options))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName)
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
        }
    })
}
/**
 * 文件上传 用于富文本上传图片
 * @param url
 * @param parameter
 * @returns {*}
 */
export function uploadAction(url, parameter) {
    return axios({
        url: url,
        data: parameter,
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data', // 文件上传
        },
    })
}

/**
 * 获取文件服务访问路径
 * @param avatar
 * @param subStr
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar, subStr) {
    if (!subStr) subStr = 'http'
    try {
        if (avatar && avatar.startsWith(subStr)) {
            return avatar;
        } else {
            if (avatar && 　avatar.length > 0 && avatar.indexOf('[') == -1) {
                return window._CONFIG['staticDomainURL'] + "/" + avatar;
            }
        }
    } catch (err) {
        return;
    }
}

// 上传附件
export function uploadAttach(options) {
    let formData = new FormData()
    if (options.data) {
        Object.keys(options.data).forEach(key => {
            formData.append(key, options.data[key])
        })
    }
    formData.append(options.filename, options.file, options.file.name)

    return axios({
        url: '/tp/attach/upload',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        timeout: options.timeout ? options.timeout : 30 * 60 * 1000,
        onUploadProgress: progressEvent => {
            const percentCompleted = Math.floor((progressEvent.loaded * 100) / progressEvent.total)
            options.onProgress({ percent: percentCompleted })
        },
        data: formData
    })
}

// 获取附件
export function getAttaches(params) {
    return axios({
        url: '/tp/attach/getAttaches',
        method: 'get',
        params: params
    })
}

export function previewAttach (params,url) {
  return axios({
    url: !!url?url:'/common/previewAttach',
    method: 'get',
    responseType: 'blob',
    timeout: 30 * 60 * 1000,
    params: params
  })
}
